package util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;

import org.apache.ibatis.session.SqlSession;

import baseDao.IpDao;
import entity.Ip;

public class IPAddress {
	/**
	 * localIp  内网IP地址
	 * outIp 外网IP地址
	 * @return
	 */
	public static boolean firstFile=false;//文件是否为新创建的
	//判断文件类型
	public static String codeString(File fileName) throws Exception{  
	    BufferedInputStream bin = new BufferedInputStream(  
	    new FileInputStream(fileName));  
	    int p = (bin.read() << 8) + bin.read();  
	    String code = null;  
	      
	    switch (p) {  
	        case 0xefbb:  
	            code = "UTF-8";  
	            break;  
	        case 0xfffe:  
	            code = "Unicode";  
	            break;  
	        case 0xfeff:  
	            code = "UTF-16BE";  
	            break;  
	        default:  
	            code = "GBK";  
	    }  
	      
	    return code;  
	}     
	public static String outIp="";
	public String localIp(){
        try {
            String localip=InetAddress.getLocalHost().getHostAddress();
            return localip;
        } catch (Exception e) {
            e.printStackTrace();
        }
		 return null;
	}
	
	public boolean outIp() throws Exception{
		System.out.println("进outIP了");
		String ipdz = getWebIp();
		System.out.println(ipdz);
		ipdz=ipdz.substring(ipdz.indexOf("\"cip\": \"")+8,ipdz.indexOf("\", \"cid\""));
		 SqlSession session=GetSqlSession.getSession();
			IpDao dao = session.getMapper(IpDao.class);
			Ip  ip1=new Ip();
			ip1.setDate(System.currentTimeMillis()-1000*2*24*3600);
			ip1.setOutIp(ipdz);
//			ip1.setLocalIp(localIp());
			int byTime = dao.selectByTime(ip1);
//			int byTime = dao.selectBylocalIp(ip1);
			if(byTime==0){
				outIp=ipdz;
				return false;
			}else{
				System.out.println("ip地址重复了,哈哈哈");
				return true;
			}
	}
	public static void main(String[] args) throws Exception {
		new IPAddress().outIp();
	}
	public static String getWebIp() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        try {
         URL url = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
         BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
         String s = "";
         StringBuffer sb = new StringBuffer("");
         String webContent = "";
         while ((s = br.readLine()) != null) {
          sb.append(s + "\r\n");
         }
         br.close();
         webContent = sb.toString();
       //  int start = webContent.indexOf("[")+1;
        // int end = webContent.indexOf("]");
       //  webContent = webContent.substring(start,end);
         System.out.println(webContent);
         return webContent;
        } catch (Exception e) {
         e.printStackTrace();
         return "error";
        }
  }
}
